package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.DateTimeUtil;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.RollingMsgSendStatus;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.RollingBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.result.RollingMsgListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.result.RollingMsgListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.RollingMsgIdForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.RollingMsgListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.RollingMsgSaveForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.RollingMsgUpdateForm;
import com.cloudbroker.bcs.platform.dav.common.constant.SysContants;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entity.Broker;
import com.cloudbroker.bcs.platform.dav.middle.entity.RollingMsg;
import com.cloudbroker.bcs.platform.dav.middle.service.BrokerService;
import com.cloudbroker.bcs.platform.dav.middle.service.RollingMsgService;
import com.cloudbroker.bcs.platform.dav.middle.util.PositionStrUtil;
import com.cloudbroker.bcs.platform.dav.middle.util.SearchUtil;

/**
 * Created by zhaojp18008 on 2016/5/16.
 */
@Service
public class RollingBizServiceImpl implements RollingBizService {
    @Resource
    private RollingMsgService rollingMsgService;
    
    @Resource
    private BrokerService brokerService;
    
    public static final Integer ROLLINGMSG_TYPE = 1;
    
    @Override
    public RollingMsgListResult funcRollingMsgListGet(RollingMsgListForm param) {
        RollingMsgListResult rollingMsgListResult = new RollingMsgListResult();
        RollingMsg queryMsg = new RollingMsg();
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        Integer tenant_id = HSBCPSBizDataContext.getTenantId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, RollingMsgListResult.class);
        }
        queryMsg.setBroker_id(broker_id);
        queryMsg.setTenant_id(tenant_id);
        queryMsg.setCreate_time(new Date());
        Integer request_num = StringUtil.isEmpty(param.getRequest_num()) ? null : Integer.parseInt(param
                .getRequest_num());
        if (null == request_num || request_num < SysContants.MIN_REQUEST_NUM
                || request_num > SysContants.MAX_REQUEST_NUM) {
            request_num = SysContants.DEFAULT_REQUSET_NUM;
        }
        queryMsg.setRequest_num(request_num);
        queryMsg.setPosition_str(param.getPosition_str());
        
        List<RollingMsg> rollingMsgList = rollingMsgService.queryMsgList(queryMsg);
        List<RollingMsgListData> rollingMsgListDataList = new ArrayList<>();
        for (RollingMsg rollingMsg : rollingMsgList) {
            RollingMsgListData rollingMsgListData = new RollingMsgListData();
            rollingMsgListData.setPosition_str(rollingMsg.getPosition_str());
            rollingMsgListData.setCreate_time(DateTimeUtil.toString(rollingMsg.getCreate_time(),
                    DateTimeUtil.FORMAT_YYYYMMDDHHMMSS));
            rollingMsgListData.setMsg_content(rollingMsg.getMsg_content());
            rollingMsgListData.setRollingmsg_id(rollingMsg.getRollingmsg_id());
            rollingMsgListData.setSend_status(rollingMsg.getSend_status());
            rollingMsgListDataList.add(rollingMsgListData);
        }
        rollingMsgListResult.setData_list(rollingMsgListDataList);
        return VOUtil.setSuccessResult(rollingMsgListResult);
        
    }
    
    /*存入滚动直播消息*/
    @Override
    public HSBCPSBackVO funcRollingMsgInfoSave(RollingMsgSaveForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, HSBCPSBackVO.class);
        }
        RollingMsg rollingMsg = new RollingMsg();
        rollingMsg.setBroker_id(broker_id);
        Date now = new Date();
        rollingMsg.setCreate_time(now);
        String position_str = PositionStrUtil.getPositionStr();
        rollingMsg.setPosition_str(position_str);
        /*设置是否发送*/
        rollingMsg.setSend_status(StringUtil.isEmpty(param.getSend_status()) ? RollingMsgSendStatus.SEND.getVal()
                : param.getSend_status());
        rollingMsg.setMsg_content(param.getMsg_content());
        rollingMsgService.save(rollingMsg);
        /*更新到搜索引擎*/
        Broker broker = brokerService.get(broker_id.toString());
        String broker_avatar = broker.getBroker_avatar();
        String broker_name = broker.getBroker_name();
        SearchUtil.getInstance().addToSearch(rollingMsg, broker_name, broker_avatar);
        return VOUtil.genSuccessResultVo();
    }
    
    /*
    *修改滚动直播消息
    * 包括是否发送，内容修改
    * */
    @Override
    public HSBCPSBackVO funcRollingMsgUpdate(RollingMsgUpdateForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, HSBCPSBackVO.class);
        }
        RollingMsg rollingMsg = rollingMsgService.get(param.getRollingmsg_id());
        if (rollingMsg == null || !broker_id.equals(rollingMsg.getBroker_id())) {
            /*消息不存在*/
            return VOUtil.genErrorResult(HSBCPSErrorCodes.RollingMsgErr.NO_SUCH_ROLLING_MSG, HSBCPSBackVO.class);
        }
        rollingMsg.setMsg_content(param.getMsg_content());
        rollingMsg.setSend_status(param.getSend_status() == null ? RollingMsgSendStatus.SEND.getVal() : param
                .getSend_status());
        String position_str = PositionStrUtil.getPositionStr();
        rollingMsg.setPosition_str(position_str);
        this.rollingMsgService.update(rollingMsg);
        //
        SearchUtil.getInstance().updateToSearch(position_str, param,rollingMsg);
        return VOUtil.genSuccessResultVo();
    }
    
    /*
    *删除滚动直播消息
    * 包括是否发送，内容修改
    * */
    @Override
    public HSBCPSBackVO funcRollingMsgDel(RollingMsgIdForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, HSBCPSBackVO.class);
        }
        
        RollingMsg rollingMsg = rollingMsgService.get(param.getRollingmsg_id());
        if (rollingMsg == null || !broker_id.equals(rollingMsg.getBroker_id())) {
            //消息不存在
            return VOUtil.genErrorResult(HSBCPSErrorCodes.RollingMsgErr.NO_SUCH_ROLLING_MSG, HSBCPSBackVO.class);
        }
        this.rollingMsgService.delete(param.getRollingmsg_id());
        //删除搜索引擎
        SearchUtil.getInstance().deleteToSearch(ROLLINGMSG_TYPE, rollingMsg.getRollingmsg_id().toString());
        return VOUtil.genSuccessResultVo();
        
    }
}
